/*
 * Copyright 2016 drakeet. https://github.com/drakeet
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.epfresh.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.universalimageloader.core.imageaware.ImageViewAware;
import com.epfresh.api.utils.T;
import com.epfresh.bean.MarketBean;

import java.util.Collections;
import java.util.List;

import multitype.OnRecyclerViewItemClickListener;

/**
 * @author drakeet
 */
public class ViewBinderMarketChildCategoryItemAdapter extends RecyclerView.Adapter<ViewBinderMarketChildCategoryItemAdapter.ViewHolder>  implements View.OnClickListener{

    private List<MarketBean.TopCateGory.ChildrenBean> posts = Collections.emptyList();
    OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;

    public OnRecyclerViewItemClickListener getOnRecyclerViewItemClickListener() {
        return onRecyclerViewItemClickListener;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }


    public void setPosts(@NonNull List<MarketBean.TopCateGory.ChildrenBean> posts) {
        this.posts = posts;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.include_market_category_child, parent, false);
        return new ViewHolder(view);
    }


    @Override
    public void onClick(View v) {
        Object o = v.getTag(R.id.item_key_status);
//        T.toast("click child category"+"_first__");
        if (onRecyclerViewItemClickListener != null && o instanceof MarketBean.TopCateGory.ChildrenBean) {
//            T.toast("click child category"+"__second___");
            onRecyclerViewItemClickListener.onItemClick(v, (Integer) v.getTag(R.id.item_key_position), o);
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        MarketBean.TopCateGory.ChildrenBean bean = posts.get(position);
        holder.tv_category_goods_name.setText(bean.getName());
        holder.rl_category_child.setTag(R.id.item_key_status,bean);
        holder.rl_category_child.setTag(R.id.item_key_position,position);
        holder.rl_category_child.setOnClickListener(this);
        ImageLoader.getInstance().displayImage(bean.getIcon(), new ImageViewAware(holder.iv_category_goods));
    }

    @Override
    public int getItemCount() {
        return posts.size();
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {

        TextView tv_category_goods_name;
        ImageView iv_category_goods;
        View rl_category_child;

        ViewHolder(View itemView) {
            super(itemView);
            iv_category_goods = (ImageView) itemView.findViewById(R.id.iv_category_goods);
            tv_category_goods_name = (TextView) itemView.findViewById(R.id.tv_category_goods_name);
            rl_category_child = itemView.findViewById(R.id.rl_category_child);
        }
    }
}
